import pandas as pd
class BoxItem:
    def __init__(self, meta_data=None):
        self.name = ''
        self.left_top = tuple([0, 0])
        self.right_bottom = tuple([0, 0])
        self.H, self.W = 0, 0
        if meta_data:
            self.construct(meta_data)

    def construct(self, meta_data: dict):
        # check the keys in meta_data.
        for k in ['name', 'rectangle']: assert k in meta_data
        coordinates = meta_data['rectangle']
        # check the keys in meta_data['rectangle'].
        for k in ['left_top', 'right_bottom']: assert k in coordinates
        self.meta_data = meta_data
        self.name = meta_data['name']
        # check the data type and value in coordinates.
        for i in coordinates['left_top']+coordinates['right_bottom']:
            if type(i) != int:
                raise ValueError("Wrong dtype of coordinates, it should be INT type.")
            elif i < 0:
                raise ValueError("Wrong value of coordinates (%d), it should be equal or greater than 0." %i)
        self.left_top = tuple(coordinates['left_top'])
        self.right_bottom = tuple(coordinates['right_bottom'])
        # compute the height and width of the box.
        self.W = coordinates['right_bottom'][0] - coordinates['left_top'][0]
        self.H = coordinates['right_bottom'][1] - coordinates['left_top'][1]

    def __str__(self):
        # to print the object.
        return "Box (%s) with rectangle: \n \t left-top coordinate: (%d, %d); right-bottom coordinate : (%d, %d) \n \t " \
               "with size: %d X %d" \
               "" %(self.name, *self.left_top, *self.right_bottom, self.W, self.H)




